﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class CellAlarm_ReasonM : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Mtitle.InnerText = ConfigurationManager.AppSettings["SystemName"].Substring(0, 6);
            MFoot.InnerText = DateTime.Now.Year.ToString() + "©"
                + ConfigurationManager.AppSettings["CompanyName"] + "Gt";
            UnLinkBind();
            OverListBind();
        }        
    }

    private void UnLinkBind()
    {
        DataSet ds = GetUnLink();
        BtnSubmit.Visible = (ds.Tables[0].Rows.Count>0);
        Msg.Visible = (ds.Tables[0].Rows.Count == 0);
        UnlinkList.DataSource = ds;
        UnlinkList.DataBind();
    }

    private void OverListBind()
    {
        string sqlStr = @"SELECT CELL_CHNAME,START_TIME,RECOVER_TIME,CELL_CLASS,MAIN_REASON
            ,SECOND_REASON,OVERREASON,ISTELE,T2.REAL_NAME AS EXAMINER FROM U_UNLINK_REASON_T T1 
            LEFT JOIN U_USER_INFO_T T2 ON T1.EXAMINER=T2.LOGIN_NAME WHERE STATUS = '驳回' 
            AND T1.EXAM_DATE > SYSDATE - 7  ";
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            sqlStr += " AND SUBSTR(CELL_CHNAME,0,2)='" + dr["company"].ToString()+"' ";
        }
        sqlStr += " ORDER BY EXAM_DATE DESC";
        OverList.DataSource = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
        OverList.DataBind();
    }

    private DataSet GetUnLink()
    {
        string sqlstr = "AB_GetReport_Pk.GetUNLinkReason";
        OracleParameter[] _params = {
            new OracleParameter("v_County",OracleType.VarChar,20),
            new OracleParameter("resultSet",OracleType.Cursor)
        };
        _params[0].Value = "";
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            _params[0].Value = dr["company"].ToString();
        }
        _params[1].Direction = ParameterDirection.Output;
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.StoredProcedure, _params);
    }

    private void BindSmartDropDownList(YYControls.SmartDropDownList yys)
    {
        ListItem li = new ListItem("—请选择—", "");
        yys.Items.Add(li);

        li = new ListItem("市电故障", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("供电线路中断", "市电故障|供电线路中断");
        yys.Items.Add(li);
        li = new ListItem("低电压", "市电故障|低电压");
        yys.Items.Add(li);
        li = new ListItem("缺相", "市电故障|缺相");
        yys.Items.Add(li);
        li = new ListItem("停电后无后备电源", "市电故障|停电后无后备电源");
        yys.Items.Add(li);
        li = new ListItem("大面积停电", "市电故障|大面积停电");
        yys.Items.Add(li);
        li = new ListItem("单站停电", "市电故障|单站停电");
        yys.Items.Add(li);
        li = new ListItem("变压器空开闸刀故障", "市电故障|变压器空开闸刀故障");
        yys.Items.Add(li);
        li = new ListItem("业主纠纷", "市电故障|业主纠纷");
        yys.Items.Add(li);


        li = new ListItem("电源柜原因", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("参数设置不当", "电源柜原因|参数设置不当");
        yys.Items.Add(li);
        li = new ListItem("一二次下电接线不当", "电源柜原因|一二次下电接线不当");
        yys.Items.Add(li);
        li = new ListItem("模块故障", "电源柜原因|模块故障");
        yys.Items.Add(li);
        li = new ListItem("电池故障", "电源柜原因|电池故障");
        yys.Items.Add(li);
        li = new ListItem("直流远供故障", "电源柜原因|直流远供故障");
        yys.Items.Add(li);

        li = new ListItem("传输设备原因", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("光端机故障", "传输设备原因|光端机故障");
        yys.Items.Add(li);
        li = new ListItem("2M线故障", "传输设备原因|2M线故障");
        yys.Items.Add(li);
        li = new ListItem("FE故障", "传输设备原因|FE故障");
        yys.Items.Add(li);
        li = new ListItem("分组设备故障", "传输设备原因|分组设备故障");
        yys.Items.Add(li);
        li = new ListItem("尾纤故障", "传输设备原因|尾纤故障");
        yys.Items.Add(li);
        li = new ListItem("微波故障", "传输设备原因|微波故障");
        yys.Items.Add(li);

        li = new ListItem("无线设备故障", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("BBU故障", "无线设备故障|BBU故障");
        yys.Items.Add(li);
        li = new ListItem("BTS故障", "无线设备故障|BTS故障");
        yys.Items.Add(li);
        li = new ListItem("RRU故障", "无线设备故障|RRU故障");
        yys.Items.Add(li);
        li = new ListItem("驻波", "无线设备故障|驻波");
        yys.Items.Add(li);
        li = new ListItem("射频模块故障", "无线设备故障|射频模块故障");
        yys.Items.Add(li);
        li = new ListItem("尾纤连接故障", "无线设备故障|尾纤连接故障");
        yys.Items.Add(li);

        li = new ListItem("光缆中断", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("一干车辆人为", "光缆中断|一干车辆人为");
        yys.Items.Add(li);
        li = new ListItem("一干自然灾害", "光缆中断|一干自然灾害");
        yys.Items.Add(li);
        li = new ListItem("二干骨干环车辆人为", "光缆中断|二干骨干环车辆人为");
        yys.Items.Add(li);
        li = new ListItem("二干骨干环自然灾害", "光缆中断|二干骨干环自然灾害");
        yys.Items.Add(li);
        li = new ListItem("主环2.5G以上车辆人为", "光缆中断|主环2.5G以上车辆人为");
        yys.Items.Add(li);
        li = new ListItem("主环2.5G以上自然灾害", "光缆中断|主环2.5G以上自然灾害");
        yys.Items.Add(li);
        li = new ListItem("本地网其他车辆人为", "光缆中断|本地网其他车辆人为");
        yys.Items.Add(li);
        li = new ListItem("本地网其他自然灾害", "光缆中断|本地网其他自然灾害");
        yys.Items.Add(li);
        li = new ListItem("本地网光缆自然损耗", "光缆中断|本地网光缆自然损耗");
        yys.Items.Add(li);

        li = new ListItem("其他", "optgroup");
        yys.Items.Add(li);
        li = new ListItem("高温原因", "其他|高温原因");
        yys.Items.Add(li);
        li = new ListItem("割接原因", "其他|割接原因");
        yys.Items.Add(li);
        li = new ListItem("工程原因", "其他|工程原因");
        yys.Items.Add(li);
        li = new ListItem("设备原因", "其他|设备原因");
        yys.Items.Add(li);
    }

    protected void UnlinkList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            YYControls.SmartDropDownList yys = (YYControls.SmartDropDownList)e.Item.FindControl("ReasonD");
            BindSmartDropDownList(yys);
        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem i in UnlinkList.Items) {
            YYControls.SmartDropDownList s = (YYControls.SmartDropDownList)i.FindControl("ReasonD");
            HiddenField net = (HiddenField)i.FindControl("Net");
            Label STARTTIME = (Label)i.FindControl("STARTTIME");
            Label RECOVERTIME = (Label)i.FindControl("RECOVERTIME");
            Label CELLCLASS = (Label)i.FindControl("CellClass");
            Label CellChname = (Label)i.FindControl("CellChname");
            CheckBox IsTele = (CheckBox)i.FindControl("IsTele");
            TextBox Remark = (TextBox)i.FindControl("Remark");
            if (s.SelectedValue.Length > 0)
            {
                string[] reasons = s.SelectedValue.Split('|');
                string sqlStr = @"SELECT COUNT(1) AS C FROM U_UNLINK_REASON_T WHERE CELL_CHNAME=:CELL_CHNAME AND START_TIME=:START_TIME AND Status IN ('上报','通过')";
                OracleParameter[] _params = {
                    new OracleParameter(":CELL_CHNAME",OracleType.NVarChar,256),
                    new OracleParameter(":START_TIME",OracleType.DateTime)
                };
                _params[0].Value = CellChname.Text;
                _params[1].Value = DateTime.Parse(STARTTIME.Text);
                int num = Int32.Parse(OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _params).ToString());
                if (num == 0)
                {
                    sqlStr = @"INSERT INTO U_UNLINK_REASON_T(CELL_CHNAME, START_TIME, RECOVER_TIME, CELL_CLASS
                                    , CELL_NET, MAIN_REASON, SECOND_REASON, INPUTTER, STATUS,IsTele,Remark)
                                VALUES(:CELL_CHNAME, :START_TIME, :RECOVER_TIME, :CELL_CLASS, :CELL_NET, :MAIN_REASON
                                    , :SECOND_REASON, :INPUTTER, '上报',:IsTele,:Remark)";
                    OracleParameter[] _paramss = {
                        new OracleParameter(":MAIN_REASON",OracleType.NVarChar,16),
                        new OracleParameter(":SECOND_REASON",OracleType.NVarChar,16),
                        new OracleParameter(":INPUTTER",OracleType.VarChar,16),
                        new OracleParameter(":CELL_CHNAME",OracleType.NVarChar,256),
                        new OracleParameter(":START_TIME",OracleType.DateTime),
                        new OracleParameter(":RECOVER_TIME",OracleType.DateTime),
                        new OracleParameter(":CELL_NET",OracleType.VarChar,8),
                        new OracleParameter(":CELL_CLASS",OracleType.NVarChar,10),
                        new OracleParameter(":IsTele",OracleType.NVarChar,2),
                        new OracleParameter(":Remark",OracleType.NVarChar,32)
                    };
                    _paramss[0].Value = reasons[0].ToString();
                    _paramss[1].Value = reasons[1].ToString();
                    _paramss[2].Value = LoginUser;
                    _paramss[3].Value = CellChname.Text;
                    _paramss[4].Value = DateTime.Parse(STARTTIME.Text);
                    _paramss[5].Value = DateTime.Parse(RECOVERTIME.Text);
                    _paramss[6].Value = net.Value;
                    _paramss[7].Value = CELLCLASS.Text;
                    _paramss[8].Value = IsTele.Checked ? "是" : "否";
                    _paramss[9].Value = Remark.Text.Trim();
                    OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _paramss);
                }
            }
        }
        UnLinkBind();
        OverListBind();
    }
}
